#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys

execfile(os.path.join(sys.path[0], "framework.py"))

from mocks import MockEntry, MockRepository
from oriolus.docker.docker import Docker
from oriolus.pith.packets.packet import Status
from unittest import TestCase, TestLoader, TextTestRunner

class TestSearch(TestCase):
    def test_search_packet1(self):
        class EmptyMockRepository(MockRepository):
            def find_packet_by_fullname(self, full_name):
                return None
        mock = EmptyMockRepository()
        docker = Docker(mock)
        result = docker.search(full_name = "test_packet-1.2.3.4")
        self.assertEqual([None], result)

    def test_search_packet2(self):
        class MockRepository1(MockRepository):
            def find_packet_by_fullname(self, full_name):
                return MockEntry("test_packet-1.2.3.4", Status.BAD)

        mock = MockRepository1()
        docker = Docker(mock)
        packets = docker.search(full_name = "test_packet-1.2.3.4");
        self.assertEqual(packets[0].name, "test_packet-1.2.3.4")
        self.assertEqual(packets[0].status, Status.BAD)

    def test_search_packet3(self):
        class MockRepository1(MockRepository):
            def find_packets_by_status(self, status):
                return [MockEntry("test_packet-1.2.3.4", Status.BAD), MockEntry("test_packet-1.2.3.4", Status.INSTALLED)]

        mock = MockRepository1()
        docker = Docker(mock)

        packets = docker.search(status = Status.BAD)
        self.assertEqual(packets[0].name, "test_packet-1.2.3.4")
        self.assertEqual(packets[0].status, Status.BAD)

    def test_search_packet4(self):
        class MockRepository1(MockRepository):
            def find_packet_by_fullname(self, full_name):
                return MockEntry("test_packet-1.2.3.4", Status.BAD)

            def find_packets_by_status(self, status):
                return [MockEntry("test_packet-1.2.3.4", Status.BAD), MockEntry("test_packet-1.2.3.6", Status.BAD)]

        mock = MockRepository1()
        docker = Docker(mock)

        packets = docker.search(status = Status.BAD, full_name = "test_packet-1.2.3.4")
        self.assertEqual(1, len(packets))
        self.assertEqual(packets[0].name, "test_packet-1.2.3.4")
        self.assertEqual(packets[0].status, Status.BAD)

    def test_search_packet5(self):
        class MockRepository1(MockRepository):
            def get_all(self):
                return [MockEntry("test_packet-1.2.3.4", Status.BAD), MockEntry("test_packet-1.2.3.5", Status.INSTALLED)]

        mock = MockRepository1()
        docker = Docker(mock)
        packets = docker.search(get_all = True)

        self.assertEqual(2, len(packets))
        self.assertEqual(packets[1].name, "test_packet-1.2.3.4")
        self.assertEqual(packets[1].status, Status.BAD)
        self.assertEqual(packets[0].name, "test_packet-1.2.3.5")
        self.assertEqual(packets[0].status, Status.INSTALLED)

    def test_search_packet6(self):
        class MockRepository1(MockRepository):
            def find_packets_by_name(self, name):
                return [MockEntry("test_packet-1.2.3.4", Status.BAD), MockEntry("test_packet-1.2.3.5", Status.BAD)]

        mock = MockRepository1()
        docker = Docker(mock)
        packets = docker.search(name = 'test_packet')

        self.assertEqual(2, len(packets))
        self.assertEqual(packets[1].name, "test_packet-1.2.3.4")
        self.assertEqual(packets[1].status, Status.BAD)
        self.assertEqual(packets[0].name, "test_packet-1.2.3.5")
        self.assertEqual(packets[0].status, Status.BAD)

if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestSearch)
    TextTestRunner(verbosity = 2).run(suite)
